local jilis = fk.CreateSkill {

  name = "joy__jilis",

  tags = {  },

}



jilis:addEffect(fk.CardUsing, {
  name = "joy__jilis",
  anim_type = "drawcard",
  events = {fk.CardUsing, fk.CardResponding,fk.TurnEnd},
  can_trigger = function(self, event, target, player, data)
    if event == fk.TurnEnd then
      return target == player and player:hasSkill(jilis.name) and not player:isNude()
    else
    if target == player and player:hasSkill(jilis.name) then
      local x, y = player:getAttackRange(), player:getMark("joy__jilis_times-turn")
      if x >= y then
        local room = player.room
        local logic = room.logic
        local end_id = player:getMark("joy__jilis_record-turn")
        local e = logic:getCurrentEvent()
        if end_id == 0 then
          local turn_event = e:findParent(GameEvent.Turn, false)
          if turn_event == nil then return false end
          end_id = turn_event.id
        end
        room:setPlayerMark(player, "joy__jilis_record-turn", logic.current_event_id)
        local events = logic.event_recorder[GameEvent.UseCard] or Util.DummyTable
        for i = #events, 1, -1 do
          e = events[i]
          if e.id <= end_id then break end
          local use = e.data
          if use.from == player.id then
            y = y + 1
          end
        end
        events = logic.event_recorder[GameEvent.RespondCard] or Util.DummyTable
        for i = #events, 1, -1 do
          e = events[i]
          if e.id <= end_id then break end
          local use = e.data
          if use.from == player.id then
            y = y + 1
          end
        end
        room:setPlayerMark(player, "joy__jilis_times-turn", y)
        return x == y
      end
    end
    end
  end,
  on_cost = function (self, event, target, player, data)
    if event == fk.TurnEnd then
      local cards = player.room:askForDiscard(player,1,1,true,jilis.name,true,".","蒺藜：你可以弃置一张牌然后摸一张牌。",true)
      if #cards > 0 then
        event:setCostData(self, cards[1])
        return true
      end
    else
      return player.room:askForSkillInvoke(player,jilis.name,nil,"是否发动〖蒺藜〗摸 "..player:getAttackRange().." 张牌？")
    end
  end,
  on_use = function(self, event, target, player, data)
    if event == fk.TurnEnd then
      player.room:throwCard(event:getCostData(self),jilis.name,player,player)
      if not player.dead then
        player:drawCards(1,jilis.name)
      end
    else
      player:drawCards(player:getAttackRange())
    end
  end,
})
jilis:addEffect(fk.CardResponding, {
  name = "joy__jilis",
  anim_type = "drawcard",
  events = {fk.CardUsing, fk.CardResponding,fk.TurnEnd},
  can_trigger = function(self, event, target, player, data)
    if event == fk.TurnEnd then
      return target == player and player:hasSkill(jilis.name) and not player:isNude()
    else
    if target == player and player:hasSkill(jilis.name) then
      local x, y = player:getAttackRange(), player:getMark("joy__jilis_times-turn")
      if x >= y then
        local room = player.room
        local logic = room.logic
        local end_id = player:getMark("joy__jilis_record-turn")
        local e = logic:getCurrentEvent()
        if end_id == 0 then
          local turn_event = e:findParent(GameEvent.Turn, false)
          if turn_event == nil then return false end
          end_id = turn_event.id
        end
        room:setPlayerMark(player, "joy__jilis_record-turn", logic.current_event_id)
        local events = logic.event_recorder[GameEvent.UseCard] or Util.DummyTable
        for i = #events, 1, -1 do
          e = events[i]
          if e.id <= end_id then break end
          local use = e.data
          if use.from == player.id then
            y = y + 1
          end
        end
        events = logic.event_recorder[GameEvent.RespondCard] or Util.DummyTable
        for i = #events, 1, -1 do
          e = events[i]
          if e.id <= end_id then break end
          local use = e.data
          if use.from == player.id then
            y = y + 1
          end
        end
        room:setPlayerMark(player, "joy__jilis_times-turn", y)
        return x == y
      end
    end
    end
  end,
  on_cost = function (self, event, target, player, data)
    if event == fk.TurnEnd then
      local cards = player.room:askForDiscard(player,1,1,true,jilis.name,true,".","蒺藜：你可以弃置一张牌然后摸一张牌。",true)
      if #cards > 0 then
        event:setCostData(self, cards[1])
        return true
      end
    else
      return player.room:askForSkillInvoke(player,jilis.name,nil,"是否发动〖蒺藜〗摸 "..player:getAttackRange().." 张牌？")
    end
  end,
  on_use = function(self, event, target, player, data)
    if event == fk.TurnEnd then
      player.room:throwCard(event:getCostData(self),jilis.name,player,player)
      if not player.dead then
        player:drawCards(1,jilis.name)
      end
    else
      player:drawCards(player:getAttackRange())
    end
  end,
})
jilis:addEffect(fk.TurnEnd, {
  name = "joy__jilis",
  anim_type = "drawcard",
  events = {fk.CardUsing, fk.CardResponding,fk.TurnEnd},
  can_trigger = function(self, event, target, player, data)
    if event == fk.TurnEnd then
      return target == player and player:hasSkill(jilis.name) and not player:isNude()
    else
    if target == player and player:hasSkill(jilis.name) then
      local x, y = player:getAttackRange(), player:getMark("joy__jilis_times-turn")
      if x >= y then
        local room = player.room
        local logic = room.logic
        local end_id = player:getMark("joy__jilis_record-turn")
        local e = logic:getCurrentEvent()
        if end_id == 0 then
          local turn_event = e:findParent(GameEvent.Turn, false)
          if turn_event == nil then return false end
          end_id = turn_event.id
        end
        room:setPlayerMark(player, "joy__jilis_record-turn", logic.current_event_id)
        local events = logic.event_recorder[GameEvent.UseCard] or Util.DummyTable
        for i = #events, 1, -1 do
          e = events[i]
          if e.id <= end_id then break end
          local use = e.data
          if use.from == player.id then
            y = y + 1
          end
        end
        events = logic.event_recorder[GameEvent.RespondCard] or Util.DummyTable
        for i = #events, 1, -1 do
          e = events[i]
          if e.id <= end_id then break end
          local use = e.data
          if use.from == player.id then
            y = y + 1
          end
        end
        room:setPlayerMark(player, "joy__jilis_times-turn", y)
        return x == y
      end
    end
    end
  end,
  on_cost = function (self, event, target, player, data)
    if event == fk.TurnEnd then
      local cards = player.room:askForDiscard(player,1,1,true,jilis.name,true,".","蒺藜：你可以弃置一张牌然后摸一张牌。",true)
      if #cards > 0 then
        event:setCostData(self, cards[1])
        return true
      end
    else
      return player.room:askForSkillInvoke(player,jilis.name,nil,"是否发动〖蒺藜〗摸 "..player:getAttackRange().." 张牌？")
    end
  end,
  on_use = function(self, event, target, player, data)
    if event == fk.TurnEnd then
      player.room:throwCard(event:getCostData(self),jilis.name,player,player)
      if not player.dead then
        player:drawCards(1,jilis.name)
      end
    else
      player:drawCards(player:getAttackRange())
    end
  end,
})

return jilis